package in.muton.edushare.components.tasks.invitations;

import in.muton.edushare.components.tasks.invitations.api.Invitation;
import in.muton.edushare.components.tasks.invitations.api.InvitationModel;
import in.muton.edushare.pages.common.EdusharePanel;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Users;

public class InvitationsPanel extends EdusharePanel {

	public InvitationsPanel(String id, IModel<Users> model) {
		super(id, model);
		this.model = model;
		add(CSSPackageResource.getHeaderContribution(InvitationsPanel.class,
				"Invitations.css"));
		m = new InvitationModel(model);
		add(new ListView<Invitation>("todoPanelLV", m) {
			private static final long serialVersionUID = -2743185675237620732L;

			@Override
			protected void populateItem(final ListItem<Invitation> arg0) {
				arg0.add(arg0.getModelObject().getDestURL());
				Form<Void> f;
				arg0.add(f = new Form<Void>("dummyForm"));
				f.add(new Button("accept") {
					@Override
					public void onSubmit() {
						arg0.getModelObject().accept();
					}

					private static final long serialVersionUID = 9005021645036602421L;
				});
				f.add(new Button("decline") {
					@Override
					public void onSubmit() {
						arg0.getModelObject().decline();
					}

					private static final long serialVersionUID = 9005021645036602421L;
				});

			}
		});
	}

	@Override
	public boolean isVisible() {
		if(m.getNumber() <= 0)
			return false;
		if(getLoggedInUser() == model.getObject())
			return true;
		return false;
	}

	private IModel<Users> model = null;
	private InvitationModel m;
	private static final long serialVersionUID = 4770022028414963567L;
}
